// SummaryView.cpp : implementation file
//

#include "stdafx.h"
#include "PADInformer.h"
#include "MainFrm.h"
#include "PADInformerDoc.h"
#include "SummaryView.h"
#include "Global.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// SummaryView

IMPLEMENT_DYNCREATE(SummaryView, CListView)

SummaryView::SummaryView()
{
}

SummaryView::~SummaryView()
{
}


BEGIN_MESSAGE_MAP(SummaryView, CListView)
	//{{AFX_MSG_MAP(SummaryView)
	ON_WM_LBUTTONDOWN()
	ON_WM_KEYDOWN()
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// SummaryView drawing

void SummaryView::OnDraw(CDC* pDC)
{
	CDocument* pDoc = GetDocument();
	// TODO: add draw code here
}

/////////////////////////////////////////////////////////////////////////////
// SummaryView diagnostics

#ifdef _DEBUG
void SummaryView::AssertValid() const
{
	CListView::AssertValid();
}

void SummaryView::Dump(CDumpContext& dc) const
{
	CListView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// SummaryView message handlers

void SummaryView::OnInitialUpdate() 
{
	CListView::OnInitialUpdate();
	
  ((CMainFrame*)AfxGetMainWnd())->m_pSummaryView = this;	

  CListCtrl& ListCtrl = GetListCtrl();

  // Insert Columns
  ListCtrl.InsertColumn(0,"No.", LVCFMT_LEFT, 45);
  ListCtrl.InsertColumn(1,"Time", LVCFMT_LEFT, 180);
  ListCtrl.InsertColumn(2,"Packet Type", LVCFMT_LEFT, 200);

  ModifyStyle(0, LVS_REPORT | LVS_SHOWSELALWAYS | LVS_SINGLESEL);
  ListCtrl.SetExtendedStyle(LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT | LVS_EX_HEADERDRAGDROP);

  m_nRefreshTimer = SetTimer(1001, 1000, NULL);
}

void SummaryView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	CListView::OnLButtonDown(nFlags, point);

	CListCtrl& ListCtrl = GetListCtrl();

  char strTemp[10];
  int  nIndex;

  nIndex = ListCtrl.GetSelectionMark();
  ListCtrl.GetItemText(nIndex, 0, strTemp, 10);
  nIndex = atoi(strTemp);
  
  DetailsView* pDetailsView = ((CMainFrame*)AfxGetMainWnd())->m_pDetailsView;
  pDetailsView->ShowDetails(nIndex);
}

void SummaryView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	CListView::OnKeyDown(nChar, nRepCnt, nFlags);

  if ((nChar == VK_UP) || (nChar == VK_DOWN)) // Up or Down key
  {
    CListCtrl& ListCtrl = GetListCtrl();

    char strTemp[10];
    int  nIndex;

    nIndex = ListCtrl.GetSelectionMark();
    ListCtrl.GetItemText(nIndex, 0, strTemp, 10);
    nIndex = atoi(strTemp);

    DetailsView* pDetailsView = ((CMainFrame*)AfxGetMainWnd())->m_pDetailsView;
    pDetailsView->ShowDetails(nIndex);
  }
}

void SummaryView::OnTimer(UINT nIDEvent)
{
	RefreshList();
  KillTimer(m_nRefreshTimer);
}

void SummaryView::RefreshList()
{
  int      i = 0;
  int      nItem;
  char     buffer[30];
  POSITION rPosition;
  PACKET   packetToAdd;

  CListCtrl& ListCtrl = GetListCtrl();

  rPosition = PacketList.GetHeadPosition();

  while (i < g_nNumberOfPackets)
  {
    PacketList.GetNext(rPosition);
    i++;
  }

  while (rPosition != NULL)
  {
    packetToAdd = PacketList.GetNext(rPosition);

    if (packetToAdd.bIncludeInListBox)
    {
      itoa(packetToAdd.nPacketNo, buffer, 10);
      nItem = ListCtrl.InsertItem(packetToAdd.nPacketNo-1, buffer);

      sprintf(buffer, packetToAdd.tPacketTime.Format(TIMEFORMAT));
      ListCtrl.SetItemText(nItem, 1, buffer);

      sprintf(buffer, packetToAdd.strPacketType);
      ListCtrl.SetItemText(nItem, 2, buffer);
    }
    g_nNumberOfPackets++;
  }
}
